#coding=utf-8
import serial
import time
import os
import threading
# import vcom
#socat -d -d pty,raw,echo=0 pty,raw,echo=0
import random
import subprocess
import sys
import atexit
def exitfunc():
    os.system("killall socat")
    print('exit is done!')
# atexit.register(exitfunc)
def vcom0():
    b=os.popen("socat -d -d pty,raw,echo=0 pty,raw,echo=0")
    print(b,type(b))
def vcom():
    b=subprocess.run("socat -d -d pty,raw,echo=0 pty,raw,echo=0",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding="utf-8")
    print(b,type(b))
def tpDemo():
    ser=serial.Serial("/dev/pts/2",9600,timeout=0.5) #使用USB连接串行口
    while(True):
        one=random.random()*16
        s="+  %02.5f g\r\n" % one
        ser.write(s.encode(encoding="utf-8"))
        time.sleep(1)
    ser.close()#关闭端口
def vcom_server():
    thread=threading.Thread(target=vcom)
    thread.start()    
def tp_server():
    thread=threading.Thread(target=tpDemo)
    thread.start()    
if __name__ == "__main__":
    # vcom.run_server()
    #tp_server()
    vcom0()
    time.sleep(3)#wait vcom
    tpDemo()
    print("end")
    

